package com.demo.config;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.demo.model.AgingNode;
import org.springframework.context.annotation.Configuration;

import java.util.*;

@Configuration
public class SendSms {


    public void send(AgingNode agingNode) {
        String accessKeyId = "LTAI5tJ8tfeoVZJdfBhDhzJH";
        String accessSecret = "rAJGtn2LGzUK4lEcT3c16Dngprhgft";
        DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers", agingNode.getTelephone());//给该手机号发送短信
        request.putQueryParameter("SignName", "非诚勿扰资产管理 ");//短信签名
        request.putQueryParameter("TemplateCode", "SMS_218031193");//短信模板code，配置文件中只需要写模板code不需要写模板内容

        Map<String, Object> map = new HashMap<>();
        map.put("item", agingNode.getClaimsName());
        map.put("name", agingNode.getBorrowers());
        map.put("trace", agingNode.getCaseMessage());
        map.put("event", agingNode.getAlarmType());
        map.put("day", String.valueOf(agingNode.getSubTime()));
        JSONObject json = new JSONObject(map);

        request.putQueryParameter("TemplateParam", json.toString());//需要的参数，参数只能使用json格式
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

    }

}